package com.qf.air.index;

import com.qf.air.entity.Air;
import com.qf.air.mapper.AirMapper;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 何星宇
 * @version V1.0
 * @Date 2021/11/3 20:46
 */

@RunWith(SpringRunner.class)
@ContextConfiguration(locations = {
        "classpath:applicationContext-mybatis.xml"
})
public class addDoc {

    @Autowired
    private AirMapper mapper;
    @Autowired
    private RestHighLevelClient restHighLevelClient;

    @Test
    public void add() throws Exception {
        //调用数据库，查询所有air信息
        List<Air> airList = mapper.findAll();
        BulkRequest bulkRequest = new BulkRequest();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        for (Air air : airList) {
            //构建request对象，设置index和id
            IndexRequest request = new IndexRequest();

            request.index("air_index");
            request.id(air.getId().toString());
            Map source = new HashMap();
            source.put("id", air.getId());
            source.put("districtId", air.getDistrictId());
            source.put("districtName", air.getDistrictName());
            source.put("monitorTime", simpleDateFormat.format(air.getMonitorTime()));
            source.put("pm10", air.getPm10());
            source.put("pm25", air.getPm25());
            source.put("monitoringStation", air.getMonitoringStation());
            source.put("lastModifyTim", simpleDateFormat.format(air.getLastModifyTime()));

            request.source(source);
            bulkRequest.add(request);
        }

        //构建批量操作bulkRequest对象，将IndexRequest放进去
        BulkResponse response = restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT);
        System.out.println(response.buildFailureMessage());


        //构建request对象，设置index和id
//        IndexRequest indexRequest1 = new IndexRequest();
//        indexRequest1.index("air_index");
//        indexRequest1.id("3");
//        Map source1 = new HashMap();
//        source1.put("districtId","3");
//        source1.put("districtName","昌平");
//        source1.put("monitorTime","2020-01-01");
//        source1.put("pm10","12");
//        source1.put("pm25","25");
//        source1.put("monitoringStation","昌平监测站");
//        source1.put("lastModifyTim","2021-11-11");
//        indexRequest1.source(source1);
//
//        IndexRequest indexRequest2 = new IndexRequest();
//        indexRequest2.index("air_index");
//        indexRequest2.id("4");
//        Map source2 = new HashMap();
//        source2.put("districtId","2");
//        source2.put("districtName","海淀");
//        source2.put("monitorTime","2020-01-01");
//        source2.put("pm10","10");
//        source2.put("pm25","20");
//        source2.put("monitoringStation","海淀监测站");
//        source2.put("lastModifyTim","2021-11-11");
//        indexRequest2.source(source2);
//
//        BulkRequest bulkRequest = new BulkRequest();
//        bulkRequest.add(indexRequest1);
//        bulkRequest.add(indexRequest2);
//
//        BulkResponse response = ElasticsearchUtil.getClient().bulk(bulkRequest, RequestOptions.DEFAULT);
//        for (BulkItemResponse item : response.getItems()) {
//            System.out.println(item.getResponse().toString());
//        }
    }
}


